package cn.zhangtiancinb.simple_demo.dubbo.consumer;

import javax.annotation.Resource;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 暴露接口
 *
 * @author 张天赐
 */
@RestController
@RequestMapping("/http")
public class ApiController {

    @Resource
    private ConsumerService consumerService;

    /**
     * 默认实现
     *
     * @param value 用于判断是否出错的参数
     * @return 函数结果
     */
    @RequestMapping("default")
    public String auto(@RequestParam(required = false, defaultValue = "0") int value) {
        return consumerService.call(value);
    }

    /**
     * 状态码总是200
     *
     * @param value 用于判断是否出错的参数
     * @return 函数结果
     */
    @RequestMapping("always200")
    public String alwaysOk(@RequestParam(required = false, defaultValue = "0") int value) {
        try {
            return consumerService.call(value);
        } catch (Exception ex) {
            return HttpStatus.INTERNAL_SERVER_ERROR.toString();
        }
    }
}
